package com.wing.cabinet.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.response.CabinetCargoLaneVO;
import com.wing.wcabinet.model.entity.WcabCellInventorySpace;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.CabinetCargoLane;
import com.wing.cabinet.model.request.CabinetCargoLaneForm;
import com.wing.cabinet.service.CabinetCargoLaneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-刀柜货道管理", tags = "#/cabinet/cabinetCargoLane")
@RestController("cabinetCargoLaneController")
@RequestMapping("cabinet/admin/cabinetCargoLane")
@RequiredArgsConstructor
public class CabinetCargoLaneController {

    private final CabinetCargoLaneService cabinetCargoLaneService;


    @ApiOperation(value = "查询所有刀柜货道", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value = "刀柜编号", defaultValue = "", example = "", dataType = "String", paramType = "query")

    })
    @GetMapping("/list")
    public PageResult<CabinetCargoLaneVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "cast(t.code as unsigned)") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String code
            , String cabCode
            , Long cabinetId
            , String queryFlag
            , Long receiveId
            , String skuCode
            , String skuName
            , String mainCabCode
            , Cabinet.CabinetNature cabinetNature
            , String wsFlag
            , String cabCode4Query
            , WcabCellInventorySpace.BusinessType businessType
            ,Integer quantityPackUnit
            ){

        Page<CabinetCargoLaneVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        //搜索
        if (StringUtils.isNotBlank(code)) {
            params.put("code", code);
        }
        if(StringUtils.isNotBlank(cabCode)){
            params.put("cabinetCode",cabCode);
        }
        params.put("cabCode",cabCode4Query);
        params.put("cabinetId",cabinetId);
        params.put("queryFlag",queryFlag);
        params.put("receiveId",receiveId);
        params.put("skuCode",skuCode);
        params.put("mainCabCode",mainCabCode);
        params.put("skuName",skuName);
        if (StringUtils.isBlank(wsFlag)){
            wsFlag=Cabinet.CabinetType.spring.name();
        }
        params.put("wsFlag",wsFlag);
        if (cabinetNature!=null){
            params.put("cabinetNature",cabinetNature.getValue());
        }
        if(businessType!=null){
            params.put("businessType",businessType.getValue());
        }
        params.put("quantityPackUnit",quantityPackUnit);
        //搜索
        Page<CabinetCargoLaneVO> list = cabinetCargoLaneService.pageList4app(pg, params);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "查询所有刀柜货道", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value = "刀柜编号", defaultValue = "", example = "", dataType = "String", paramType = "query")

    })
    @GetMapping("/listCheck")
    public PageResult<CabinetCargoLaneVO> list4Check(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "cast(t.code as unsigned)") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String code
            , String cabCode
            , Long cabinetId
            , String queryFlag
            , Long receiveId
            , String skuCode
            , String mainCabCode
            , Cabinet.CabinetNature cabinetNature
            , String wsFlag
            , String cabCode4Query
            , WcabCellInventorySpace.BusinessType businessType
    ){

        Page<CabinetCargoLaneVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        //搜索
        if (StringUtils.isNotBlank(code)) {
            params.put("code", code);
        }
        if(StringUtils.isNotBlank(cabCode)){
            params.put("cabinetCode",cabCode);
        }
        params.put("cabCode",cabCode4Query);
        params.put("cabinetId",cabinetId);
        params.put("queryFlag",queryFlag);
        params.put("receiveId",receiveId);
        params.put("skuCode",skuCode);
        params.put("mainCabCode",mainCabCode);
        if (StringUtils.isBlank(wsFlag)){
            wsFlag=Cabinet.CabinetType.spring.name();
        }
        params.put("wsFlag",wsFlag);
        if (cabinetNature!=null){
            params.put("cabinetNature",cabinetNature.getValue());
        }
        if(businessType!=null){
            params.put("businessType",businessType.getValue());
        }
        //搜索
        Page<CabinetCargoLaneVO> list = cabinetCargoLaneService.pageList4Check(pg, params);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加刀柜货道", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(CabinetCargoLaneForm.Add.class) CabinetCargoLaneForm cabinetCargoLaneForm) {
        if (cabinetCargoLaneService.add(cabinetCargoLaneForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改刀柜货道", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(CabinetCargoLaneForm.Update.class) CabinetCargoLaneForm cabinetCargoLaneForm) {
        CabinetCargoLane cabinetCargoLane = cabinetCargoLaneService.getById(cabinetCargoLaneForm.getId());
        if(cabinetCargoLane == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CabinetCargoLane.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(cabinetCargoLaneForm, cabinetCargoLane, copyOptions);
        if (cabinetCargoLaneService.updateById(cabinetCargoLane)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除刀柜货道", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cabinetCargoLaneId", value = "刀柜货道id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long cabinetCargoLaneId) {
        if (cabinetCargoLaneService.removeById(cabinetCargoLaneId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    /**
     * 获取本归所有可用货道
     *
     * @return {@link JsonResult}<{@link CabinetCargoLaneVO}>
     */
    @GetMapping("/getMyCabinetCargoLane")
    public JsonResult<CabinetCargoLane> getMyCabinetCargoLane(@RequestParam("cabCode") String cabCode,@RequestParam("skuCode") String skuCode){
        QueryWrapper<CabinetCargoLane> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cab_code",cabCode);
        queryWrapper.eq("product_sku",skuCode);
        List<CabinetCargoLane> list = cabinetCargoLaneService.list(queryWrapper);

        QueryWrapper<CabinetCargoLane> queryWrappers = new QueryWrapper<>();
        queryWrappers.eq("cab_code",cabCode);
        queryWrappers.isNull("product_sku");
        List<CabinetCargoLane> lists = cabinetCargoLaneService.list(queryWrappers);
        list.addAll(lists);
        return JsonResult.success(list);
    }

}
